package com.spzx.product.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.GlobalException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {
    @Autowired
    CategoryBrandMapper categoryBrandMapper;

    @Autowired
    ICategoryService categoryService;

    /**
     * 分类品牌分页
     *
     * @param categoryBrand
     * @return
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        List<CategoryBrand> categoryBrandList = categoryBrandMapper.selectCategoryBrandList(categoryBrand);
        return categoryBrandList;
    }

    /**
     * 根据id获取详细信息
     *
     * @param id
     * @return
     */
    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);
        //三级分类下拉需要回显，只返回第三级分类id是不能回显三个级别的分类的。必须将三个级别分类id都返回才能正常回显。
        //三个级别的id必须按照顺序返回：【一级id、二级id、三级id】  【1,2,3】
        //根据指定分类id，查找当前分类id，以及全部祖先id
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    /**
     * 新增分类品牌
     *
     * @param categoryBrand
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertCategoryBrand(CategoryBrand categoryBrand) {
        //保存前需要校验：如果关联数据已经存在，那么不能保存成功。
        Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()).eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
        if (count > 0) {
            throw new GlobalException("数据已经存在！");
        }

        return categoryBrandMapper.insert(categoryBrand);
    }

    /**
     * 修改分类品牌
     *
     * @param categoryBrand
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateCategoryBrand(CategoryBrand categoryBrand) {
        //保存前需要校验：如果关联数据已经存在，那么不能保存成功。
        CategoryBrand categoryBrandDB = categoryBrandMapper.selectOne(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
        //判断查出来的categoryBrandDB是否为空，并且判断id和前端传过来的对象ID是否相等，相等依然不让再次保存
        if (categoryBrandDB != null && categoryBrandDB.getId().intValue() != categoryBrand.getId().intValue()) {
            throw new GlobalException("数据已经存在！");
        }
        return categoryBrandMapper.updateById(categoryBrand);
    }

    /**
     * 根据分类id获取品牌列表
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> selectCategoryBrandByCategoryId(Long categoryId) {
        return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
    }
}
